#include "Including.h"

using namespace std;


namespace Collada
{

	/**
	 * Render the collada model
	 */
	void MyCollada::RenderCollada (const struct COLLADA *colladafile)
	{
		if(collada_flat == "wrong")
			return;

		struct Float_Array f[1]; //Float Array
		struct P pp[10]; //P 
		float **vertices; //Saving vertices
		unsigned short *triangles; //Saving triangle position
		int number_geometrices;
		int number_triangles;
		int number_inputs;

		//for loop: Geometry
		number_geometrices = colladafile->m_GeometryLibrary[0].number_geometrices;
		for(int geo = 0; geo < number_geometrices; geo++) {

		f[0].m_ID = colladafile->m_GeometryLibrary[0].m_Geometries[geo].m_Mesh[0].m_Sources[0].m_Float_Array[0].m_ID; //float_array id
		f[0].m_Count = colladafile->m_GeometryLibrary[0].m_Geometries[geo].m_Mesh[0].m_Sources[0].m_Float_Array[0].m_Count; //float_array count
		f[0].m_Floats = colladafile->m_GeometryLibrary[0].m_Geometries[geo].m_Mesh[0].m_Sources[0].m_Float_Array[0].m_Floats; //float_array values

		//initialise vertices
		int row = f[0].m_Count/3;
		vertices = new float*[3];
		for(int i = 0; i < 3; i++)
		{ vertices[i] = new float[row]; }

		//transfer actual values from float_array to vertices
		int j = 0; 
		for(int i = 0; i < row; i++)
		{
			vertices[0][i] = f[0].m_Floats[j];
			vertices[1][i] = f[0].m_Floats[j+1];
			vertices[2][i] = f[0].m_Floats[j+2];
			j = j + 3;
		}

		//Drawing Triangles
		number_triangles = colladafile->m_GeometryLibrary[0].m_Geometries[geo].m_Mesh[0].number_triangles; 
		for(int z = 0; z < number_triangles; z++)
		{
			number_inputs = colladafile->m_GeometryLibrary[0].m_Geometries[geo].m_Mesh[0].m_Triangles[z].number_inputs; //how many inputs
			pp[z].m_Count = colladafile->m_GeometryLibrary[0].m_Geometries[geo].m_Mesh[0].m_Triangles[z].m_Count * ( 3 * number_inputs); //P count
			pp[z].m_Indices = colladafile->m_GeometryLibrary[0].m_Geometries[geo].m_Mesh[0].m_Triangles[z].m_p[0].m_Indices; //P index

			//select and save triangle vertex indices only
			triangles = new unsigned short[pp[z].m_Count/number_inputs];
			int k = 0;
			int p_count = pp[z].m_Count/number_inputs; 
			for(int i = 0; i < p_count; i++)
			{
				triangles[i] = pp[z].m_Indices[k];
				k = k + number_inputs;
			}

			//start to draw collada models
			glPushMatrix();
			//glTranslatef(-5,-5,-10);
			//glRotatef(-90,1,0,0);
			//glScalef(0.1,0.1,0.1);
			glBegin (GL_TRIANGLES);
			for(int y = 0; y < p_count; y = y + 3){
				glVertex3f(vertices[0][triangles[y]],vertices[1][triangles[y]],vertices[2][triangles[y]]);
				glVertex3f(vertices[0][triangles[y+1]],vertices[1][triangles[y+1]],vertices[2][triangles[y+1]]);
				glVertex3f(vertices[0][triangles[y+2]],vertices[1][triangles[y+2]],vertices[2][triangles[y+2]]);
			}
			glEnd ();	
			glPopMatrix();
			//release triangles array
			delete triangles;
		}

		//release vertices matrix
		for(int i = 0; i < 3; i++)
		{ delete vertices[i]; }
		delete vertices;

		} //geo

	}
}